﻿#include <iostream>
#include <string>
using namespace std;
class Rational
{
private:
	int numerator;//fenzi
	int denominator;//fenmu
	int flag;//用于标记正负数 默认0 正数 1 负数
	double value;
public:
	Rational();
	Rational(int);
	Rational(int i , int j );
	double GetValue();
	friend ostream& operator<<(ostream&, Rational&);
	//Rational operator+(Rational);
	friend Rational operator+(Rational&,Rational&);
	friend Rational operator+(Rational&, int);
	friend Rational operator+(int,Rational&);
	friend Rational operator-(Rational&);
	friend Rational operator++(Rational&, int);//后置 
	friend Rational operator++(Rational&);//前置
	operator double() const;
};
double Rational:: GetValue()
{
	cout << double(numerator) / double(denominator) << endl;
	return double(numerator) / double(denominator);
}
Rational::Rational()
{
	this->flag = 0;
	this->denominator = 1;
	this->numerator = 1;
	this->value = static_cast<double>(numerator)/static_cast<double>(denominator);//如果不强制转换，是先计算然后再转换成double，损失精度
	//this->value = double(denominator) / double(numerator);
	//cout << value << endl;
}
Rational::Rational(int i)
{
	if (i < 0)
	{
		flag = 1;
	}
	this->numerator = i;
	this->denominator = 1;
	this->value = i;
}
Rational::Rational(int i, int j)
{
	if (j)//j不为0
	{
		this->numerator = i;
	}
	else
	{
		cout << "Denominator Cannot be 0" << endl;
		this->numerator = 1;
	}
	this->denominator = j;
	this->value = static_cast<double>(numerator) / static_cast<double>(denominator);//如果不强制转换，是先计算然后再转换成double，损失精度
}
ostream& operator<<(ostream& os, Rational& r)
{
	//os << r.numerator << "/" << r.denominator << endl;
	os << r.value;
	return os;
}
Rational operator+(Rational&r1, Rational&r2)
{
	Rational r;
	r.denominator = r1.value + r2.value;
	return r;
}
Rational operator+(Rational& r, int i)
{
	r.value += static_cast<double>(i);
	return r;
}
Rational operator+(int i, Rational&r)
{
	r.value += static_cast<double>(i);
	return r;
}
Rational operator++(Rational& r)
{
	r.denominator++;
	r.numerator++;
	r.value= static_cast<double>(r.numerator) / static_cast<double>(r.denominator);
	return r;
}
Rational operator++(Rational& r, int i)
{
	Rational temp;
	r.denominator++;
	r.numerator++;
	//r.value = static_cast<double>(r.numerator) / static_cast<double>(r.denominator);
	r.value++;
	return temp;
}
Rational operator-(Rational& r)
{
	//r.denominator = -r.denominator;
	//r.numerator = -r.numerator;
	r.value =- r.value;
	return r;
}
Rational::operator double()const
{
	return double(this->value);
}
int main()
{
	Rational rat0;
	cout << rat0 << endl;
	Rational rat1 = -rat0;
	cout << rat1 << endl;
	Rational rat2 = rat1 + 34;
	cout << rat2 << endl;
	Rational rat3;
	rat3 = 12 + rat2;
	cout << rat3 << endl;
	rat3++;
	cout << rat3 << endl;
	cout << double(rat3) << endl;
	double sum=0;
	for (int i = 1; i < 50; i++)
	{
		Rational r(i, i + 1);
		//sum += r.GetValue();
		sum += r;
	}
	cout << sum << endl;
}
